# -*-  coding:utf-8 -*-
# 主要用来测试selenium使用phantomJs
import random
import time
# 导入webdriver
from urllib import parse
from time import sleep
from selenium import webdriver
# 要想调用键盘按键操作需要引入keys包
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.common.keys import Keys

from agent import header
from doubanbook1.doubanbook.mylib.agent import user_agent_list, referer_list


class myselenium(object):
    def __init__(self, url):
        # 调用环境变量指定的PhantomJS浏览器创建浏览器对象
        driver = webdriver.Chrome(executable_path=r"C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chromedriver.exe")
        # driver = webdriver.PhantomJS()
        driver.set_window_size(1366, 768)
        # 如果没有在环境变量指定PhantomJS位置
        # driver = webdriver.PhantomJS(executable_path = "./phantomjs")

        # get方法会一直等到页面加载，然后才会继续程序，通常测试会在这里选择time.sleep(2)

        driver.get(url)

        # 模拟用户操作
        for j in range(20):
            js3 = 'window.scrollTo(' + str(j * 1280) + ',' + str((j + 1) * 1280) + ')'
            driver.execute_script(js3)  # 执行js语言滚动滚动条
            time.sleep(1)
            driver.save_screenshot('myselenium{0}.png'.format(j))
        # 向下滚动10000像素
        # js = "document.body.scrollTop=10000"
        # # js="var q=document.documentElement.scrollTop=10000"
        # time.sleep(3)

        # 查看页面快照
        driver.save_screenshot("douban.png")

        # 执行JS语句
        driver.execute_script(js)
        # 停一会，一面操作太快被识别出来
        time.sleep(10)

        # 查看页面快照
        driver.save_screenshot("newdouban.png")
        # # 获取页面名为wraper的id标签的文本内容
        # data = driver.find_element_by_id('wrapper').text
        #
        # # 打印数据内容
        # print(data)
        #
        # print(driver.title)
        #
        # # 生成页面快照并保存
        # time.sleep(3)


        # # id="kw"是百度搜索输入框，输入字符串"长城"
        # driver.find_element_by_id('kw').send_keys(u'长城')
        #
        # # id="su"是百度搜索按钮，click()是模拟点击
        # driver.find_element_by_id('su').click()
        # time.sleep(3)
        # # 获取新的页面快照
        # driver.save_screenshot("长城.png")
        #
        # # 打印网页渲染后的源代码
        # print(driver.page_source)
        #
        # # 获取当前页面Cookie
        # print(driver.get_cookies())
        #
        # # ctrl+a全选输入框内容
        # driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a')
        # # ctrl+x剪切输入框内容
        # driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'x')
        #
        # # 输入框重新输入内容
        # driver.find_element_by_id('kw').send_keys('肺炎')
        #
        # # 模拟Enter回车键
        # driver.find_element_by_id('su').send_keys(Keys.RETURN)
        # time.sleep(5)
        #
        # # 清空输入框内容
        # driver.find_element_by_id('kw').clear()
        #
        # # 生成新的页面快照
        # driver.save_screenshot('itcast.png')
        #
        # # 获取当前url
        # print(driver.current_url)
        #
        # driver.quit()

    def test(self):
        # 调用环境变量指定的PhantomJS浏览器创建浏览器对象
        driver = webdriver.PhantomJS()
        driver.set_window_size(1366, 768)
        # 如果没有在环境变量指定PhantomJS位置
        # driver = webdriver.PhantomJS(executable_path = "./phantomjs")

        # get方法会一直等到页面加载，然后才会继续程序，通常测试会在这里选择time.sleep(2)

        driver.get("http://www.baidu.com/")

        # 获取页面名为wraper的id标签的文本内容
        data = driver.find_element_by_id('wrapper').text

        # 打印数据内容
        print(data)

        print(driver.title)

        # 生成页面快照并保存
        driver.save_screenshot("baidu.png")

        # id="kw"是百度搜索输入框，输入字符串"长城"
        driver.find_element_by_id('kw').send_keys(u'长城')

        # id="su"是百度搜索按钮，click()是模拟点击
        driver.find_element_by_id('su').click()
        time.sleep(3)
        # 获取新的页面快照
        driver.save_screenshot("长城.png")

        # 打印网页渲染后的源代码
        print(driver.page_source)

        # 获取当前页面Cookie
        print(driver.get_cookies())

        # ctrl+a全选输入框内容
        driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a')
        # ctrl+x剪切输入框内容
        driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'x')

        # 输入框重新输入内容
        driver.find_element_by_id('kw').send_keys('肺炎')

        # 模拟Enter回车键
        driver.find_element_by_id('su').send_keys(Keys.RETURN)
        time.sleep(5)

        # 清空输入框内容
        driver.find_element_by_id('kw').clear()

        # 生成新的页面快照
        driver.save_screenshot('itcast.png')

        # 获取当前url
        print(driver.current_url)

        driver.quit()


dest = '自慰器'


# https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&wq=%E6%89%8B%E6%9C%BA&pvid=437813fa882441649140337a8488d965
def item(name=dest, page=1, s=30):
    return 'https://search.jd.com/Search?keyword={0}&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq={0}&page={1}&s={2}&psort=3&click=0'.format(
        parse.quote(name), page, s)  # url编码


if __name__ == '__main__':
    url = 'https://blog.csdn.net/dh1151313194/article/details/103642912'
    # ok = myselenium(item(name=dest, page=1, s=30))

    # 如果没有在环境变量指定PhantomJS位置
    # driver = webdriver.PhantomJS(executable_path = "./phantomjs")

    # get方法会一直等到页面加载，然后才会继续程序，通常测试会在这里选择time.sleep(2)
    for i in range(0,799):
        # .driver = webdriver.PhantomJS(executable_path="./phantomjs")

        dcap = dict(DesiredCapabilities.PHANTOMJS)
        dcap["phantomjs.page.settings.userAgent"] = random.choice(user_agent_list)
        dcap["phantomjs.page.settings.referer"] = random.choice(referer_list)
        # driver = webdriver.PhantomJS(desired_capabilities=dcap)
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('user-agent={0}'.format(random.choice(user_agent_list)))
        chrome_options.add_argument('referer={0}'.format(random.choice(referer_list)))
        driver = webdriver.Chrome(keep_alive=False, chrome_options=chrome_options,
                                  executable_path=r"C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chromedriver.exe")


        print(i)
        driver.get(url=url,)
        sleep(0.1)
        for j in range(2):
            js3 = 'window.scrollTo(' + str(j * 1280) + ',' + str((j + 1) * 1280) + ')'
            driver.execute_script(js3)  # 执行js语言滚动滚动条
            # time.sleep(1)
            driver.save_screenshot('myselenium{0}.png'.format(j))
        # 查看phantom的详细参数
        cap_dict = driver.desired_capabilities
        for key in cap_dict:
            print('%s: %s' % (key, cap_dict[key]))
        driver.quit()
